package com.cci.kangdao.escalation.controller;

import com.alibaba.fastjson.JSON;
import com.cci.kangdao.aop.RequestBodyThreadLocal;
import com.cci.kangdao.dto.PagedRequest;
import com.cci.kangdao.escalation.dto.request.GetEscalationOrderListRequest;
import com.cci.kangdao.escalation.dto.request.SubmitReasonRequest;
import com.cci.kangdao.escalation.dto.request.SubmitSolutionRequest;
import com.cci.kangdao.escalation.dto.response.GetEscalationOrderDetailResponse;
import com.cci.kangdao.escalation.service.EscalationChannelService;
import com.cci.kangdao.escalation.service.EscalationService;
import com.cci.kangdao.service.impl.SMSEnum;
import com.cci.kangdao.utilTool.JsonUtils;
import com.cci.kangdao.utilTool.ParameterTool;
import com.cci.kangdao.utilTool.RequestContextHolder;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

/**
 * @author William Du
 * @date 2020/4/29
 */
@Controller
@RequestMapping("/escalation")
public class EscalationController {

    private org.apache.log4j.Logger log = Logger.getLogger(this.getClass().getName());

    @Resource
    private EscalationService escalationService;
    @Resource
    private EscalationChannelService escalationChannelService;

    @RequestMapping(value = "/getEscalationSupport.aspx", method = RequestMethod.GET)
    public void getEscalationSupport(@RequestParam(value = "workOrderId", required = false) Long workOrderId) {
        escalationChannelService.getEscalationSupport(workOrderId);
    }

    @RequestMapping(value = "/getEscalationReason.aspx", method = RequestMethod.GET)
    public void getEscalationReasonList(@RequestParam("type") String type,
                                        @RequestParam("roleNo") String roleNo,
                                        @RequestParam(value = "workOrderId") Long workOrderId) {
        escalationChannelService.getEscalationReason(type, roleNo, workOrderId);
    }

    @RequestMapping(value = "/submitEscalation.aspx", method = RequestMethod.POST)
    public void submitEscalation() {
        try {
            String requestBody = RequestBodyThreadLocal.get();
            SubmitReasonRequest request = JsonUtils.toJavaObject(requestBody, SubmitReasonRequest.class);
            escalationService.submitEscalation(request);
        } catch (RuntimeException e) {
            log.error(e.getMessage(), e);
            //主动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            ParameterTool.writeErrorResponse(RequestContextHolder.getResponse(), -22);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            //主动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            ParameterTool.writeErrorResponse(RequestContextHolder.getResponse(), -99);
        }
    }

    @RequestMapping(value = "/getEscalationOrderList.aspx", method = RequestMethod.GET)
    public void getEscalationOrderList(@RequestParam("page") Integer page,
                                       @RequestParam("pageSize") Integer pageSize,
                                       @RequestParam(value = "keyword", required = false) String keyword,
                                       @RequestParam(value = "roleNo") String roleNo,
                                       @RequestParam(value = "roleId", required = false) String roleId,
                                       @RequestParam(value = "orderStatus") String orderStatus) {    //0 全部;1 未完成(待反馈);2 已完成;3(进行中)

        PagedRequest pagedRequest = new PagedRequest();
        pagedRequest.setPage(page);
        pagedRequest.setPageSize(pageSize);
        GetEscalationOrderListRequest getEscalationOrderListRequest = new GetEscalationOrderListRequest(keyword, orderStatus, roleNo, roleId);
        escalationService.getEscalationOrderList(pagedRequest, getEscalationOrderListRequest);
    }

    @RequestMapping(value = "/getEscalationOrderDetail.aspx", method = RequestMethod.GET)
    public void workOrderFlowTime(@RequestParam("orderId") Long orderId,
                                  @RequestParam("roleId") Long roleId,
                                  @RequestParam("roleNo") String roleNo) {
        String roleStr = "R0041, R0069, R0033, R0074, R0091";
        if (!roleStr.contains(roleNo)) {
            ParameterTool.writeErrorResponse(RequestContextHolder.getResponse(), -25);
            return;
        }

        //获取详情
        if (null == orderId) {
            //缺少必要参数
            ParameterTool.writeResponse(RequestContextHolder.getResponse(), SMSEnum.type17.getIndex(), SMSEnum.type17.getName());
            return;
        }
        GetEscalationOrderDetailResponse detailResponse = escalationService.getEscalationOrderDetail(orderId, roleNo);
        ParameterTool.writeSuccessResponse(RequestContextHolder.getResponse(), detailResponse);
    }

    @RequestMapping(value = "/getWorkOrderListForEscalation.aspx", method = RequestMethod.GET)
    public void getWorkOrderList(@RequestParam(value = "keyword", required = false) String keyword,
                                 @RequestParam(value = "roleNo") String roleNo,
                                 @RequestParam(value = "roleId") String roleId,
                                 @RequestParam(value = "page") Integer page,
                                 @RequestParam(value = "pageSize") Integer pageSize) {
        PagedRequest pagedRequest = new PagedRequest();
        pagedRequest.setPage(page);
        pagedRequest.setPageSize(pageSize);
        escalationService.getWorkOrderListForEscalation(pagedRequest, keyword, roleNo);
    }

    @RequestMapping(value = "remindFeedback.aspx", method = RequestMethod.GET)
    public void remindFeedback(@RequestParam(value = "roleNo") String roleNo,
                               @RequestParam(value = "roleId") Long roleId,
                               @RequestParam(value = "orderId") Long orderId,
                               @RequestParam(value = "type") Integer type,
                               @RequestParam(value = "number", required = false) String number) {
        try {
            escalationService.remindFeedback(orderId, type, number);
        } catch (RuntimeException e) {
            ParameterTool.writeErrorResponse(RequestContextHolder.getResponse(), -22);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(RequestContextHolder.getResponse(), -99);
        }
    }

    @RequestMapping(value = "submitEscalationSolution.aspx", method = RequestMethod.POST)
    public void submitEscalationSolution() {
        String requestBody = RequestBodyThreadLocal.get();
        SubmitSolutionRequest request = JSON.parseObject(requestBody, SubmitSolutionRequest.class);
        try {
            escalationService.submitEscalationSolution(request);
        } catch (RuntimeException e) {
            ParameterTool.writeErrorResponse(RequestContextHolder.getResponse(), -22);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(RequestContextHolder.getResponse(), -99);
        }
    }

    @RequestMapping(value = "confirmEscalationSolution.aspx", method = RequestMethod.GET)
    public void confirmEscalationSolution(@RequestParam(value = "roleNo") String roleNo,
                                          @RequestParam(value = "roleId") Long roleId,
                                          @RequestParam(value = "orderId") Long orderId) {
        try {
            escalationService.confirmEscalationSolution(roleNo, orderId);
        } catch (RuntimeException e) {
            ParameterTool.writeErrorResponse(RequestContextHolder.getResponse(), -22);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            ParameterTool.writeErrorResponse(RequestContextHolder.getResponse(), -99);
        }
    }

    @RequestMapping(value = "disableEscalationByWorkOrderId.aspx", method = RequestMethod.GET)
    public void disableEscalationByWorkOrderId(@RequestParam(value = "workOrderId") Long workOrderId,
                                               @RequestParam(value = "type") Integer type) {
        escalationService.disableEscalationByWorkOrderId(workOrderId, type);
    }
}
